package com.example.demo;

import com.bumptech.glide.Glide;
import com.example.demo.base.Global;
import com.example.demo.msic.S;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Image;
import ohos.agp.components.TextField;

public class LoginAbility extends Ability {
    private Image mImage;
    private TextField mTfAccount;
    private TextField mTfPassword;
    private Button mBtnLogin;
    private Button mBtnFinish;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
        initView();
        initData();
    }

    private void initView() {
        mTfAccount = (TextField) findComponentById(ResourceTable.Id_tf_acccount);
        mTfPassword = (TextField) findComponentById(ResourceTable.Id_tf_password);
        mImage = (Image) findComponentById(ResourceTable.Id_iv_logo);
        mBtnFinish = (Button) findComponentById(ResourceTable.Id_btn_finish);
        mBtnLogin = (Button) findComponentById(ResourceTable.Id_btn_login);
        mBtnLogin.setClickedListener(component -> btnLoginClick());
        mBtnFinish.setClickedListener(component -> finish());
    }

    private void initData() {
        Glide.with(this)
                .load(S.url)
                .into(mImage);
    }

    private void btnLoginClick() {
        String account = mTfAccount.getText().trim();
        String password = mTfPassword.getText().trim();
        if (account.equals("admin") && password.equals("123")) {
            Global.showToast("恭喜你答对了，不愧是一个程序员。");
            return;
        }
        Global.showToast(" a: " + account + " p:  " + password);
    }

    public void finish() {
        terminateAbility();
    }

}

